<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="close">
    <el-form ref="addDept" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" placeholder="请选择负责人" style="width: 80%" size="mini">
          <!-- 下拉选项 -->
          <!-- :label为选项名称  :value为选项的值 -->
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" placeholder="1-100个字符" type="textarea" size="mini" :rows="4" style="width: 80%" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="btnOK">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getDepartment, getManagerList, addDepartment, getDepartmentDetail, updateDepatrment } from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [], // 部门负责人列表
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        managerId: '', // 部门负责人
        introduce: '', // 部门介绍
        pid: '' // 父部门ID
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称要在2-10个字符之间', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              let result = await getDepartment()
              if (this.formData.id) {
                // 编辑模式 需要排除自己
                result = result.filter(item => item.id !== this.formData.id)
              }
              if (result.some(item => item.name === value)) {
                callback(new Error('部门名称已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码要在2-10个字符之间', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              // value 就是输入的编码
              let result = await getDepartment()
              // 判断是否是编辑模式
              if (this.formData.id) {
                // 编辑模式 需要排除自己
                result = result.filter(item => item.id !== this.formData.id)
              }
              // result数组中是否存在value的值
              if (result.some(item => item.code === value)) {
                // 存在
                callback(new Error('部门编码已存在'))
              } else {
                // 不存在
                callback()
              }
            }
          }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍要在1-100个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    close() {
      // resetFields重置表单只能重置在模板中绑定的数据
      this.$refs.addDept.resetFields()
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        managerId: '', // 部门负责人
        introduce: '', // 部门介绍
        pid: '' // 父部门ID
      }
      // 修改父组件的值，完成子传父
      this.$emit('update:showDialog', false)
    },
    async getManagerList() {
      // 获取部门负责人列表
      this.managerList = await getManagerList()
    },
    btnOK() {
      // 校验表单
      this.$refs.addDept.validate(async isOK => {
        let msg = '新增'
        if (isOK) {
          if (this.formData.id) {
            // 编辑模式
            msg = '编辑'
            await updateDepatrment(this.formData)
          } else {
            // 新增模式
            await addDepartment({ ...this.formData, pid: this.currentNodeId })
          }
          // 通知父组件刷新数据
          this.$emit('updateDepartment')
          // 提示消息
          this.$message.success(`${msg}部门成功`)
          // 关闭弹层
          this.close()
        }
      })
    },
    async getDepartmentDetail() {
      // 获取部门详情
      const result = await getDepartmentDetail(this.currentNodeId)
      // 赋值给表单
      this.formData = result
    }
  }
}
</script>
